package com.rocksdata.mc.business;

import android.support.annotation.NonNull;

import com.ctz.commonframe.utils.Logger;
import com.ctz.commonframe.utils.SharedPrefrencesUtil;
import com.rocksdata.mc.BuildConfig;
import com.rocksdata.mc.SharedPreferencesKey;
import com.rocksdata.mc.database.AnnouncementDBManager;
import com.rocksdata.mc.database.DatabaseManager;
import com.rocksdata.mc.entity.AnnouncementInfo;
import com.rocksdata.mc.entity.AnnouncementPageInfo;
import com.rocksdata.mc.http.HttpClient;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by 陈堂正 on 2017/4/13.
 */

public class AnnouncementManager implements BusinessInterface.IAnnouncementManager {
    private static final String URL_GET_ANNOUNCEMENT_LIST = BuildConfig.HOST + "/notices/getNotices";

    private static BusinessInterface.IAnnouncementManager instance;
//    private AnnouncementDBManager dbManager;

    private AnnouncementManager(){
//        dbManager = DatabaseManager.getAnnouncementDBManager();
    }

    synchronized static BusinessInterface.IAnnouncementManager getInstance(){
        if(instance == null)
            instance = new AnnouncementManager();
        return instance;
    }

    @Override
    public void getAnnouncementListRemote(int pageIndex, final Integer type, final String lastDate, final BusinessInterface.OnBusinessResultListener listener){
        Map<String, Object> map = new HashMap<>();
        if (type != null)
            map.put("type", type);
        if (pageIndex > 1)
            map.put("page", pageIndex);
//        map.put("userID", UserInfoManager.getCurrentUserId());
        HttpClient.get(URL_GET_ANNOUNCEMENT_LIST, map, AnnouncementPageInfo.class, new HttpClient.OnHttpResultListener<AnnouncementPageInfo>(){

            @Override
            public void onResult(boolean isSuccess, List<AnnouncementPageInfo> data, String msg) {
                if(isSuccess){
                    List<AnnouncementInfo> list = data.get(0).getDataList();
                    setReadFlag(list);
                    listener.onBusinessResult(true, list, null);
                }else{
                    listener.onBusinessResult(false, null, msg);
                }
            }
        });

    }

    @Override
    public void setRead(String id){
//        dbManager.setRead(id);
        Set<String> alreadyReadIds = getAlreadyReadAnnouncementIds();
        if (alreadyReadIds == null) {
            alreadyReadIds = new HashSet<>();
        }
        alreadyReadIds.add(id);
        setAlreadyReadAnnouncementIds(alreadyReadIds);
    }

    private Set<String> getAlreadyReadAnnouncementIds() {
        return SharedPrefrencesUtil.getSetAboutUser(UserInfoManager.getCurrentUserId(), SharedPreferencesKey.KEY_ALREADY_READ_ANNOUNCEMNETS);
    }

    private void setAlreadyReadAnnouncementIds(@NonNull Set<String> set) {
        SharedPrefrencesUtil.putSetAboutUser(UserInfoManager.getCurrentUserId(), SharedPreferencesKey.KEY_ALREADY_READ_ANNOUNCEMNETS, set);
    }

    @Override
    public void setToTop(String id){

    }

    @Override
    public void setReadFlag(List<AnnouncementInfo> list) {
        if (list != null) {
            Set<String> alreadyReadIds = getAlreadyReadAnnouncementIds();
            if (alreadyReadIds != null) {
                for (AnnouncementInfo info : list) {
                    Logger.e("announcement", "code:" + info.getNoticeCode() + ",title:" + info.getTitle());
//                info.setRead(dbManager.isRead(info.getNoticeCode()));
                    info.setRead(alreadyReadIds.contains(info.getNoticeCode()));
                }
            }
        }
    }

    @Override
    public void sortList(List<AnnouncementInfo> list) {

    }

}
